﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using CrossEntropyProject;

namespace ProfileApplication
{
    internal class Profile
    {
        internal ChartArea chartArea1;
        internal Legend legend1;
        internal Series series1;
        internal Title title1;
        internal Chart chart1;

        internal ProfileData pf;

        #region Constructor
        public Profile(ProfileData prof, bool LoadRegularDataFlag)
        {
            pf = prof;
            if (LoadRegularDataFlag)
            {
                InitializeColumnChartComponents();
                LoadRegularData();
            }
            else
            {
                InitializeErrorBarComponents();
                LoadStandartDiviationData();
            }
        } 
        #endregion

        #region LoadRegularData()
        public void LoadRegularData()
        {
            // Calculate the x,y values of the chart
            String[] xValue = new string[pf.meansOfProfile.Length];

            for (int i = 0; i < xValue.Length; i++)
            {
                xValue[i] = "h" + (i + 1).ToString();
            }

            double[] yValue = new double[pf.meansOfProfile.Length];

            for (int i = 0; i < yValue.Length; i++)
            {

                yValue[i] = Math.Round(pf.meansOfProfile[i], 3, MidpointRounding.AwayFromZero);
            }

            chart1.Series["Series1"].Points.DataBindXY(xValue, yValue);

            for (int i = 0; i < xValue.Length; i++)
            {
                chart1.Series["Series1"].Points[i].Color = Constants.clArr[i];
                chart1.Series["Series1"].Points[i].LegendText = i.ToString();
            }

        }
        #endregion

        #region InitializeColumnChartComponents()
        public void InitializeColumnChartComponents()
        {
            // 
            // chart1
            // 
            chartArea1 = new ChartArea();
            legend1 = new Legend();
            series1 = new Series();
            title1 = new Title();
            chart1 = new Chart();
            chart1.BorderlineColor = Color.Black;
            chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            chart1.BorderlineWidth = 1;
            chart1.BackgroundImageLayout = ImageLayout.Zoom;
            chart1.BackImageAlignment = ChartImageAlignmentStyle.Center;
            chartArea1.AxisX.IsLabelAutoFit = false;
            chartArea1.AxisX.LabelStyle.Font = new Font("Microsoft Sans Serif", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
            chartArea1.AxisX.MajorGrid.Enabled = false;
            chartArea1.AxisX.MajorGrid.LineWidth = 0;
            chartArea1.AxisY.IsLabelAutoFit = false;
            chartArea1.AxisY.LabelStyle.Font = new Font("Microsoft Sans Serif", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
            chartArea1.AxisY.MajorGrid.Enabled = false;
            chartArea1.AxisY.Maximum = 1D;
            chartArea1.AxisY.Minimum = 0D;
            chartArea1.AxisY2.ArrowStyle = AxisArrowStyle.Triangle;
            chartArea1.AxisY2.MajorGrid.Enabled = false;
            chartArea1.AxisY2.Maximum = 1D;
            chartArea1.AxisY2.Minimum = 0D;
            chartArea1.BackColor = Color.Transparent;
            chartArea1.Name = "ChartArea1";
            chart1.ChartAreas.Add(chartArea1);

            chart1.ChartAreas[0].AxisY.LineWidth = 1;
            chart1.ChartAreas[0].AxisX.LineWidth = 1;
            legend1.Enabled = false;
            legend1.Name = "Legend1";
            chart1.Legends.Add(legend1);
            chart1.Name = "chart1";
            series1.BorderColor = Color.Black;
            series1.ChartArea = "ChartArea1";
            series1.CustomProperties = "PointWidth=0.5";
            series1.IsXValueIndexed = true;
            series1.Font = new Font("Microsoft Sans Serif", 12F, FontStyle.Regular, GraphicsUnit.Point, 0);
            series1.IsValueShownAsLabel = true;
            series1.LabelToolTip = "Label";
            series1.Legend = "Legend1";
            series1.Name = "Series1";
            series1.Palette = ChartColorPalette.SemiTransparent;
            series1.YValuesPerPoint = 4;
            chart1.Series.Add(series1);

            chart1.Series["Series1"]["PointWidth"] = "0.4";
            //chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";
            chart1.Size = new Size(326, 322);
            chart1.TabIndex = 0;
            chart1.Text = "chart1";
            title1.Alignment = ContentAlignment.TopCenter;
            title1.Font = new Font("Times New Roman", 15.75F, FontStyle.Italic, GraphicsUnit.Point, ((byte)(0)));
            title1.Name = "Title1";
            chart1.Titles.Add(title1);
        }
        #endregion

        #region LoadStandartDiviationData()
        public void LoadStandartDiviationData()
        {
            double[] means = pf.meansOfProfile;
            double[] stdvalues = pf.stdValues;

            String[] xValue = new string[means.Length];

            for (int i = 0; i < xValue.Length; i++)
            {
                xValue[i] = "h" + (i + 1).ToString();
            }

            //Place the values in the chart...
            //chart1.Series["Series1"].Points.AddXY(xValue, CenterMarkerInErrorBar, LowerBoundsInErrorBar, UpperBoundsInErrorBar);
            for (int i = 0; i < means.Length; i++)
            {
                chart1.Series["Series1"].Points.AddXY(xValue[i], means[i], means[i] - stdvalues[i], means[i] + stdvalues[i]);
                chart1.Series["Series1"].Points[i].Label = (Math.Round(stdvalues[i], 3, MidpointRounding.AwayFromZero)).ToString();
            }
        }
        #endregion

        #region  InitializeErrorBarComponents()
        public void InitializeErrorBarComponents()
        {
            // 
            // chart1
            // 
            chartArea1 = new ChartArea();
            legend1 = new Legend();
            series1 = new Series();
            title1 = new Title();
            chart1 = new Chart();
            chartArea1.AxisX.IsLabelAutoFit = false;
            chartArea1.AxisX.LabelStyle.Font = new Font("Microsoft Sans Serif", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 0);
            chartArea1.AxisX.MajorGrid.Enabled = false;
            chartArea1.AxisX.TitleFont = new Font("Microsoft Sans Serif", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 0);
            chartArea1.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.NotSet;
            chartArea1.AxisY.Interval = 0.1D;
            chartArea1.AxisY.MinorTickMark.Enabled = false;
            chartArea1.AxisY.Maximum = 1.2D;
            chartArea1.AxisY.Minimum = 0D;
            chartArea1.AxisY.MinorGrid.Enabled = true;
            chartArea1.AxisY.MinorGrid.LineColor = Color.LightGray;
            chartArea1.AxisY.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
            chartArea1.Name = "ChartArea1";
            chart1.ChartAreas.Add(chartArea1);
            legend1.Enabled = false;
            legend1.Name = "Legend1";
            chart1.Legends.Add(legend1);
            chart1.Location = new Point(289, 54);
            chart1.Name = "chart1";
            series1.ChartArea = "ChartArea1";
            series1.ChartType = SeriesChartType.ErrorBar;
            series1.Color = Color.Black;
            series1.CustomProperties = "ErrorBarCenterMarkerStyle=Cross";
            series1.Font = new Font("Microsoft Sans Serif", 12F, FontStyle.Regular, GraphicsUnit.Point, 0);
            series1.Legend = "Legend1";
            series1.MarkerBorderWidth = 2;
            series1.MarkerColor = Color.Red;
            series1.MarkerSize = 15;
            series1.Name = "Series1";
            series1.YValuesPerPoint = 3;
            series1.ToolTip = "The red X is the mean value, the lines are the std value";
            chart1.Series.Add(series1);
            chart1.Size = new Size(326, 322);
            chart1.TabIndex = 2;
            chart1.Text = "chart1";
            title1.Alignment = ContentAlignment.TopCenter;
            title1.Font = new Font("Times New Roman", 15.75F, FontStyle.Italic, GraphicsUnit.Point, ((byte)(0)));
            title1.Name = "Title1";
            chart1.Titles.Add(title1);
        } 
        #endregion

    }//class
}//namespace
